package com.runmi.webchat.aftersale.web.iframe.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.runmi.webchat.aftersale.common.page.PageModel;
import com.runmi.webchat.aftersale.common.result.Result;
import com.runmi.webchat.aftersale.common.result.ResultFactory;
import com.runmi.webchat.aftersale.dbcore.entity.User;
import com.runmi.webchat.aftersale.util.Constants;
import com.runmi.webchat.aftersale.web.base.BaseController;
import com.runmi.webchat.aftersale.web.iframe.model.MainLeft;
import com.runmi.webchat.aftersale.web.iframe.service.MainService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Controller
public class MainController extends BaseController{

	@Autowired
	private MainService mainService;
	
	@RequestMapping(value = "/")
	public ModelAndView main(HttpServletRequest request,HttpServletResponse response){		
		
		User user= this.getUserInfo(request, response);
		if(null==user){
			 ModelAndView mv = new ModelAndView("redirect:/login");
			 return mv;
		}else{
			ModelAndView mav = new ModelAndView("/iframe/Main/main");
			
			MainLeft model=new MainLeft();
			model.setUserId(this.getUserId(request, response));
			model.setUser(this.getUserInfo(request, response));
			mainService.left(model);
			mav.addObject("menus", model.getMenus());
			User userInfo=this.getUserInfo(request, response);
			mav.addObject("userInfo",userInfo);
			return mav;
		}
	}
//	@ApiOperation(value = "框架", notes = "框架", tags={ "iframe", })
//	@RequestMapping(value = "/welcome")
//	public ModelAndView welcome(){		
//		ModelAndView mav = new ModelAndView("/iframe/Main/welcome");
//		mav.addObject("totalCount",1111);
//		mav.addObject("cpage",2);
//        return mav;
//	}
		
//	@ApiOperation(value = "左侧菜单", notes = "获取用户左侧菜单", tags={ "框架", })
//	@RequestMapping(value = "/left",produces = { "application/json" },method = RequestMethod.GET)
//	public Result  left(
//			@ApiParam(value = "用户认证信息",required = true)@RequestHeader(value = "tocken", required = true) String tocken
//			){	
//		
////		ModelAndView mav = new ModelAndView("/iframe/Main/left");		
////		MainLeft model=new MainLeft();
////		model.setUser(this.getUserInfo(request, response));
//		return  mainService.left(this.getUserInfo(tocken));
////		mav.addObject("model", model);
////        return mav;
//	}
	
	@ApiOperation(value = "用户信息", notes = "用户信息", tags={ "框架", })
	@RequestMapping(value = "/getUserInfo",produces = { "application/json" },method = RequestMethod.GET)
	public Result header(@ApiParam(value = "用户认证信息",required = true)@RequestHeader(value = "tocken", required = true) String tocken){		
		ModelAndView mav = new ModelAndView("/iframe/Main/header");
		User model=this.getUserInfo(tocken);
		//model.setErpUser(this.getUserInfo(request, response));
		//mainService.header(model);
		return ResultFactory.getResultSuccess(model);
	}
	
	
//	
//	@ApiOperation(value = "框架", notes = "框架", tags={ "iframe", })
//	@RequestMapping(value = "/header")
//	public ModelAndView header(HttpServletRequest request,HttpServletResponse response){		
//		ModelAndView mav = new ModelAndView("/iframe/Main/header");
//		ScmUser model=this.getUserInfo(request, response);
//		//model.setErpUser(this.getUserInfo(request, response));
//		//mainService.header(model);
//		mav.addObject("model", model);
//        return mav;
//	}
	
	@RequestMapping(value = "/modifypwd")
	public ModelAndView modifypwd(HttpServletRequest request,HttpServletResponse response){		
		ModelAndView mav = new ModelAndView("/iframe/Main/modifypwd");
        return mav;
	}
	@ApiOperation(value = "修改登录密码", notes = "修改登录密码", tags={ "框架", })
	@RequestMapping(value = "/savePwd")
	@ResponseBody
	public Map<String,String> modifyPwd(
			HttpServletRequest request,HttpServletResponse response,
			@ApiParam(value = "旧密码") @RequestParam(value = "oldPwd") String oldPwd,
			@ApiParam(value = "新密码") @RequestParam(value = "newPwd1") String newPwd1,
			@ApiParam(value = "新密码") @RequestParam(value = "newPwd2") String newPwd2
			 ){
		Map<String,String> result =new HashMap<String,String>();
		try{
			result=mainService.modifyPwd(this.getUserInfo(request,response), oldPwd, newPwd1, newPwd2);
		}catch(Exception ex){
			ex.printStackTrace();
			result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
			result.put(Constants.RETURN_MESSAGE_KEY, ex.getMessage());		
		}
		return result;
	}
	
	
}
